﻿using Common.Models;
using Core.Entities.EQ;
using Core.Interfaces;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Infrastructure.Services.EQ
{
    /// <summary>
    /// 设备信息服务
    /// </summary>
    public class DeviceService : BaseService<Device, string>
    {
        private readonly IDapperRepository _dapperRepository;

        public DeviceService(
            IBaseRepository<Device, string> repository,
            IPagedRepository<Device, string> pagedRepository,
            IDapperRepository dapperRepository)
            : base(repository, pagedRepository)
        {
            _dapperRepository = dapperRepository;
        }

        /// <summary>
        /// 根据设备编码查询
        /// </summary>
        /// <param name="devCode">设备编码</param>
        /// <returns>设备信息列表</returns>
        public async Task<Device> GetByDevCodeAsync(string devCode)
        {
            string sql = @"select * from EQ_Device where DEV_CODE = '"+devCode+"'";

            var result = await _dapperRepository.QueryFirstOrDefaultAsync<Device>(sql);
            return result;
        }

        /// <summary>
        /// 根据设备名称模糊查询
        /// </summary>
        /// <param name="devName">设备名称</param>
        /// <returns>设备信息列表</returns>
        public async Task<IEnumerable<Device>> GetByDevNameAsync(string devName)
        {
            var filter = new QueryFilter
            {
                Conditions = new List<FilterCondition>
                {
                    new FilterCondition { Field = "DEV_NAME", Operator = "like", Value = devName }
                }
            };

            var result = await _pagedRepository.GetPagedAsync(filter);
            return result.Data;
        }

        /// <summary>
        /// 根据设备信息查询
        /// </summary>
        /// <returns>设备信息列表</returns>
        public async Task<PagedResult<Device>> GetByDeviceAsync(QueryFilter filter, Device device)
        {
            // 初始化条件列表
            filter.Conditions = new List<FilterCondition>();

            // 添加设备编码模糊查询条件
            if (!string.IsNullOrEmpty(device.DevCode))
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "DevCode",
                    Operator = "like",
                    Value = device.DevCode
                });
            }

            // 添加设备名称模糊查询条件
            if (!string.IsNullOrEmpty(device.DevName))
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "DevName",
                    Operator = "like",
                    Value = device.DevName
                });
            }

            // 添加产线名称查询条件
            if (!string.IsNullOrEmpty(device.ProductionLine))
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "ProductionLine",
                    Operator = "like",
                    Value = device.ProductionLine
                });
            }

            // 添加设备状态查询条件
            if (!string.IsNullOrEmpty(device.DevStatus))
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "DevStatus",
                    Operator = "=",
                    Value = device.DevStatus
                });
            }

            // 添加设备管理人查询条件
            if (!string.IsNullOrEmpty(device.DevManager))
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "DevManager",
                    Operator = "like",
                    Value = device.DevManager
                });
            }

            // 始终添加未删除的条件
            filter.Conditions.Add(new FilterCondition
            {
                Field = "IsDeleted",
                Operator = "=",
                Value = false
            });

            var result = await _pagedRepository.GetPagedAsync(filter);
            return result;
        }

        /// <summary>
        /// 批量导入设备数据
        /// </summary>
        /// <param name="devices">设备列表</param>
        /// <returns>导入结果</returns>
        public async Task<bool> ImportDevicesAsync(List<Device> devices)
        {
            try
            {
                foreach (var device in devices)
                {
                    // 检查设备编码是否已存在
                    var existingDevice = await GetByDevCodeAsync(device.DevCode);
                    if (existingDevice != null)
                    {
                        device.CreateTime = DateTime.Now;
                        // 如果存在，则更新设备信息
                        await PartialUpdateAsync(device);
                    }
                    else
                    {
                        // 如果不存在，则新增设备
                        await CreateAsync(device);
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}